package com.base.commons.log.jaeger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.base.commons.tools.utils.StringUtils;
import com.base.commons.tools.utils.UuidUtils;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class JaegerUtils {

    public static final String ORDER_TAG = "orderCode";
    public static final String ORDERS_TAG = "orderCodes";
    public static final String DESC_TAG = "desc";

    public static String getTraceId() {
        String traceId = "";
        Tracer tracer = GlobalTracer.get();
        if(tracer!=null && tracer.scopeManager().activeSpan()!=null){
            traceId = tracer.scopeManager().activeSpan().context().toTraceId();
        }else{
            traceId = UuidUtils.generateUUID();
        }

        return traceId;
    }

    /**
     * 匹配以("fieldName":")开头,以(")结尾的字符串，并且存储到分组中
     * 正则表达式匹配字段值
     * 不包含空值
     * (?<=(href=")) 表示 匹配以(href=")开头的字符串，并且捕获(存储)到分组中
     * (?=(">)) 表示 匹配以(">)结尾的字符串，并且捕获(存储)到分组中
     *
     * @param jsonStr
     * @param fieldName
     * @return
     */
    public static String getFieldListFromJsonStr(String jsonStr, String fieldName) {
        String fieldValue = "";
        if(StringUtils.isNotEmpty(jsonStr)) {
            String regex = "(?<=(\"" + fieldName + "\":\")).*?(?=(\"))";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(jsonStr);
            while (matcher.find()) {
                if (StringUtils.isNotEmpty(matcher.group().trim())) {
                    fieldValue = matcher.group().trim();
                    break;
                }
            }
        }
        return fieldValue;
    }

    /**
     * 匹配以("fieldName":[")开头,以("])结尾的字符串，并且存储到分组中
     * 正则表达式匹配字段值
     * 不包含空值
     * (?<=(href=")) 表示 匹配以(href=")开头的字符串，并且捕获(存储)到分组中
     * (?=(">)) 表示 匹配以(">)结尾的字符串，并且捕获(存储)到分组中
     *
     * @param jsonStr
     * @param fieldName
     * @return
     */
    public static String getFieldsFromJsonStr(String jsonStr, String fieldName) {
        String fieldValue = "";
        if(StringUtils.isNotEmpty(jsonStr)) {
            String regex = "(?<=(\"" + fieldName + "\":))\\[[^\\[]*\\]";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(jsonStr);
            while (matcher.find()) {
                if (StringUtils.isNotEmpty(matcher.group().trim())) {
                    JSONArray jsonArray =  JSONObject.parseArray(matcher.group().trim());
                    if(jsonArray!=null && jsonArray.size()>0) {
                        fieldValue = jsonArray.getString(0);
                        break;
                    }
                }
            }
        }
        return fieldValue;
    }

    public static void close(){
        Tracer tracer = GlobalTracer.get();
        Span span = tracer.activeSpan();
        if(span!=null){
            span.finish();
        }
    }
}